home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Examples / FileSearchTest / MainWinController.m < prev    next >
Encoding:
Text File  |  1995-04-12  |  6.4 KB  |  276 lines

  1. //        Written by Todd Thomas Copyright (c) 1994 by Todd Thomas.
  2. //                Version 1.0.  All rights reserved.
  3. //
  4. //        This notice may not be removed from this source code.
  5. //
  6. //    This object is included in the MiscKit by permission from the author
  7. //    and its use is governed by the MiscKit license, found in the file
  8. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  9. //    for a list of all applicable permissions and restrictions.
  10. //    
  11.  
  12. #import <misckit/MiscFile.h>
  13. #import <misckit/MiscStopwatch.h>
  14. #import <misckit/MiscSwapView.h>
  15. #import "MainWinController.h"
  16.  
  17.  
  18. @implementation MainWinController
  19.  
  20. // Create the object that will do the searching, and set us as the delegate,
  21. // which will be used for multiple file searches.
  22.  
  23. - init
  24. {
  25.     [super init];
  26.     infoPanel = nil;
  27.     return self;
  28. }
  29.  
  30.  
  31.  
  32. - free
  33. {
  34. //    [fileSearch free];
  35.     return [super free];
  36. }
  37.  
  38.  
  39.  
  40. // Executed when the "Begin Search" button was clicked. Make sure that all 
  41. // fields that have to have values are fine, then do the search.
  42.  
  43. - beginSearch:sender
  44. {
  45.   BOOL  recursive, followLinks;
  46.   id  pathname = nil;
  47.   id  stopWatch = nil;
  48.   id  fileSearch;
  49.   int  i;
  50.   
  51.       if (strcmp ([dirToSearch stringValue], "") == 0)
  52.     {
  53.         NXRunAlertPanel ("Insufficient Data", "You have not selected a directory to search.", "OK", 0, 0);
  54.         return self;
  55.      }
  56.  
  57.     // get the search options
  58.     recursive = [ [optionsMatrix cellAt: 0 : 0] state];
  59.     followLinks = [ [optionsMatrix cellAt: 1 : 0] state];
  60.  
  61.     fileSearch = [ [MiscFile alloc] initWithPath: [dirToSearch stringValue] ];
  62.     if (fileSearch == nil)
  63.     {
  64.         NXRunAlertPanel ("No way!", "Directory does not exist.", "OK", 0, 0);
  65.         return self;
  66.      }
  67.             
  68.     // which search is done depends upon which cell is selected in
  69.     // the popupList
  70.     
  71.     switch ([ [ [popup itemList] selectedCell] tag])
  72.     {
  73.       case 1:            // do a single filename search
  74.           
  75.         // make sure that you have been given a filename to search for
  76.         
  77.         if (strcmp ([fileToSearchFor stringValue], "") == 0)
  78.         {
  79.             NXRunAlertPanel ("Insufficient Data", "You have not selected a filename to search for.", "OK", 0, 0);
  80.             return self;
  81.           }
  82.             
  83.         [updator setStringValue: "Searching..."];
  84.         [timerField setStringValue: ""];
  85.         stopWatch = [ [ [MiscStopwatch alloc] init] startTiming: self];
  86.         
  87.         // do the search (MiscString returned if success, else nil)
  88.         
  89.         pathname = [fileSearch searchFor: [fileToSearchFor stringValue] 
  90.                 recursive: recursive followLinks: followLinks];
  91.  
  92.         // update the UI         
  93.         [stopWatch pauseTiming: self];
  94.         [timerField setFloatValue: (float)[stopWatch minute]*60.0 + 
  95.                 (float)[stopWatch second] + 
  96.                 (float)[stopWatch microsecond]*1.0e-6];
  97.  
  98.         [stopWatch free];
  99.         [self writeResults: pathname];
  100.         
  101.         if (pathname)
  102.         {
  103.             [updator setStringValue: ""];
  104.             if (pathname != fileSearch)
  105.                 [pathname free];
  106.          }
  107.         else
  108.             [updator setStringValue: "Filename was not found."];
  109.     
  110.         break;
  111.  
  112.       // do a multiple filename search (by extension)
  113.       
  114.       case 2:            
  115.       
  116.           // make sure that the user gave an extension for the search
  117.         
  118.         if (strcmp ([extensionTextfield stringValue], "") == 0)
  119.         {
  120.             NXRunAlertPanel ("Insufficient Data", "You have not entered an extension to search for.", "OK", 0, 0);
  121.             return self;
  122.           }
  123.       
  124.           [updator setStringValue: "Searching..."];
  125.         [timerField setStringValue: ""];
  126.         stopWatch = [ [ [MiscStopwatch alloc] init] startTiming: self];
  127.         
  128.         // do the actual search (List of MiscStrings returned, or nil)
  129.         
  130.           pathname = [fileSearch searchFilesAndRecurse: recursive 
  131.                         followLinks: followLinks];
  132.  
  133.         // update the UI
  134.         
  135.         [stopWatch pauseTiming: self];
  136.         [timerField setFloatValue: (float)[stopWatch minute]*60.0 + 
  137.                 (float)[stopWatch second] + 
  138.                 (float)[stopWatch microsecond]*1.0e-6];
  139.         [stopWatch free];
  140.  
  141.         [self writeResults: pathname];                
  142.         if (pathname)
  143.         {
  144.             [updator setStringValue: ""];
  145.             for (i=0; i<[pathname count]; i++)
  146.                 if ([pathname objectAt: i] != fileSearch)
  147.                     [ [pathname removeObjectAt: i] free];
  148.          }
  149.         else
  150.             [updator setStringValue: "No matches were found."];
  151.       break;
  152.         
  153.       default:
  154.           NXRunAlertPanel ("Oops.", "Did you alter the popupList??.", "OK",
  155.                 0, 0);
  156.      }
  157.     
  158.     [fileSearch free];        
  159.     return self;
  160. }
  161.  
  162.  
  163.  
  164. // Set the directory to search via the OpenPanel.
  165.  
  166. - setDirectoryToSearch:sender
  167. {
  168.   id  openPanel = [OpenPanel new];
  169.   
  170.       [openPanel chooseDirectories: YES];
  171.     [openPanel setDirectory: NXHomeDirectory()];
  172.     
  173.     if ([openPanel runModal] == NX_OKTAG)
  174.         [dirToSearch setStringValue: [openPanel filename] ];
  175.         
  176.     return self;
  177. }
  178.  
  179.  
  180.  
  181. // Tries to write the result to the text contained within the scrollview.
  182. // It will write the contents of either a single or list of MiscStrings.
  183.  
  184. - writeResults: result
  185. {
  186.   id  text = [scrollView docView];
  187.   
  188.       [text setText: ""];
  189.     
  190.     if (result == nil)
  191.         return self;
  192.         
  193.     if ([result class] == [List class])
  194.     {
  195.       int  i, length;
  196.       
  197.           for (i=0; i<[result count]; i++)
  198.         {
  199.             length = [text textLength];
  200.             [text setSel: length : length];
  201.             if (length > 0)
  202.                 [text replaceSel: "\n"];    
  203.             [text replaceSel: [ [result objectAt: i] fullPath] ];
  204.          }
  205.      }
  206.     
  207.     if ([result respondsTo: @selector(stringValue)])
  208.         [text setText: [result stringValue] ];
  209.     if ([result respondsTo: @selector(fullPath)])
  210.         [text setText: [result fullPath] ];
  211.  
  212.     return self;
  213. }
  214.  
  215.  
  216.  
  217. - showInfoPanel: sender
  218. {
  219.     if (!infoPanel)
  220.         infoPanel = [NXApp loadNibSection: "info.nib" owner: self];
  221.         
  222.     [infoPanel makeKeyAndOrderFront: self];
  223.  
  224.     return self;
  225. }
  226.  
  227. @end
  228.  
  229.  
  230.  
  231. @implementation MainWinController (Initialization)
  232.  
  233. // Sets the updator textfield to nothing. (It gets set to "Searching..." when
  234. // a search is taking place. Also ask the swapView to set itself to the 
  235. // initial view.
  236.  
  237. - awakeFromNib
  238. {
  239.     [updator setStringValue: ""];
  240.     [swapView setUseBuffering: YES];
  241.     [swapView swapContentView: self];
  242.  
  243.     // Change the popup outlet from the cover to the actual popupList
  244.     popup = [popup target];
  245.  
  246.     [ [MiscFile class] setClassDelegate: self];
  247.     return self;
  248. }
  249.  
  250. @end
  251.  
  252.  
  253.  
  254. @implementation MainWinController (MiscFileSearchingDelegate)
  255.  
  256. // This is the only method that a FileSearch delegate implements. Every
  257. // filename in the search directory is sent here. For this example, we just 
  258. // check the extension of the filename. If it matches what was entered 
  259. // in the UI return YES (which adds it to the list of filenames returned).
  260.  
  261. - (BOOL)addFile: (MiscFile *)theFile 
  262. {
  263.       char  *givenExtension = (char *)[extensionTextfield stringValue];
  264.  
  265.     if (givenExtension == NULL || [theFile extension] == NULL)
  266.         return NO;
  267.         
  268.     if (strcmp (givenExtension, [theFile extension]) == 0)
  269.         return YES;
  270.  
  271.     return NO;
  272. }
  273.  
  274. @end
  275.  
  276.